package com.lilei.bigdata.mr.join;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单表和商品表联查
 *
 * order.txt(订单id, 日期, 商品编号, 数量)
 * 	1001	20150710	P0001	2
 * 	1002	20150710	P0001	3
 * 	1002	20150710	P0002	3
 * 	1003	20150710	P0003	3
 * product.txt(商品编号, 商品名字, 类别, 价格)
 * 	P0001	小米5	1001	2
 * 	P0002	锤子T1	1000	3
 * 	P0003	锤子	1002	4
 */
public class RJoin {
    static class RJoinMapper extends Mapper<LongWritable, Text,Text,InfoBean>{
        InfoBean infoBean =new InfoBean();
        Text k = new Text();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] fields = value.toString().split(",");

            FileSplit fileSplit = (FileSplit) context.getInputSplit();
            String name = fileSplit.getPath().getName();
            String pid = "";
            if (name.startsWith("order")){
                pid =fields[2];
                infoBean.set(Integer.parseInt(fields[0]), fields[1],pid,Integer.parseInt(fields[3]),"",0,0,"0");
            }else {
                pid =fields[0];
                infoBean.set(0, "",pid,0,fields[1],Integer.parseInt(fields[2]),Float.parseFloat(fields[3]),"1");
            }
            k.set(pid);
            context.write(k,infoBean);
        }
    }

    static class RJoinReducer extends Reducer<Text,InfoBean,InfoBean, NullWritable>{
        @Override
        protected void reduce(Text key, Iterable<InfoBean> values, Context context) throws IOException, InterruptedException {
            List<InfoBean> infoBeans = new ArrayList<InfoBean>();
            InfoBean pdBean = new InfoBean();
            for (InfoBean infoBean:values){
                if ("0".equals(infoBean.getFlag())){   //订单
                    InfoBean odBean = new InfoBean();
                    try {
                        BeanUtils.copyProperties(odBean,infoBean);
                        infoBeans.add(odBean);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }else{
                    try {
                        BeanUtils.copyProperties(pdBean,infoBean);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }


            for (InfoBean odbean:infoBeans){
                odbean.setCategory_id(pdBean.getCategory_id());
                odbean.setPname(pdBean.getPname());
                odbean.setPrice(pdBean.getPrice());

                context.write(odbean,NullWritable.get());
            }
        }
    }


    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();

        conf.set("mapred.textoutputformat.separator", "\t");

        Job job = Job.getInstance(conf);

        // 指定本程序的jar包所在的本地路径
        // job.setJarByClass(RJoin.class);
 		job.setJar("I:/workspace/idea/bigdata/day06_hadoop/target/day06_hadoop-1.0-SNAPSHOT.jar");

    //    job.setJarByClass(RJoin.class);
        // 指定本业务job要使用的mapper/Reducer业务类
        job.setMapperClass(RJoinMapper.class);
        job.setReducerClass(RJoinReducer.class);

        // 指定mapper输出数据的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(InfoBean.class);

        // 指定最终输出的数据的kv类型
        job.setOutputKeyClass(InfoBean.class);
        job.setOutputValueClass(NullWritable.class);

        // 指定job的输入原始文件所在目录
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        // 指定job的输出结果所在目录
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        // 将job中配置的相关参数，以及job所用的java类所在的jar包，提交给yarn去运行
        /* job.submit(); */
        boolean res = job.waitForCompletion(true);
        System.exit(res ? 0 : 1);

    }
}
